package edu.xidian.bdilab.jrbigdata.service.government.businessStatics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.government.GovernmentCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.*;
import edu.xidian.bdilab.jrbigdata.mapper.*;
import edu.xidian.bdilab.jrbigdata.service.government.businessStatics.BusinessStaticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BusinessStaticsServiceImpl implements BusinessStaticsService {

    @Autowired
    BankInfoMapper bankInfoMapper;

    @Autowired
    FinancialProductMapper financialProductMapper;

    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    EnterpriseReqMapper enterpriseReqMapper;

    @Autowired
    ProductRequestMapper productRequestMapper;

    @Override
    public ResponseResult getBusinessStatics() throws Exception{
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(14);
        //银行数量
        QueryWrapper<BankInfo> bankInfoQueryWrapper=new QueryWrapper<>();
        data.put("BankNum",bankInfoMapper.selectCount(bankInfoQueryWrapper));
        //产品数量
        QueryWrapper<FinancialProduct> financialProductQueryWrapper=new QueryWrapper<>();
        List<FinancialProduct> financialProductList;
        financialProductList=financialProductMapper.selectList(null);
        data.put("FinacialProductNum",financialProductList.size());
        //企业数量
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper=new QueryWrapper<>();
        data.put("EnterpriseNum",enterpriseInfoMapper.selectCount(enterpriseInfoQueryWrapper));

        //企业公开需求数量
        QueryWrapper<EnterpriseReq> enterpriseReqQueryWrapper=new QueryWrapper<>();
        enterpriseReqQueryWrapper.eq("is_public", 0);
        List<EnterpriseReq> enterpriseReqList;
        enterpriseReqList=enterpriseReqMapper.selectList(enterpriseReqQueryWrapper);
        data.put("EnterpriseReqNum",enterpriseReqList.size());
        //企业公开需求金额
        int enteriseReqCount=0;
        for(EnterpriseReq enterpriseReq:enterpriseReqList){
            switch (enterpriseReq.getLoanLimit()){
                case 0:enteriseReqCount+=100;break;
                case 1:enteriseReqCount+=200;break;
                case 2:enteriseReqCount+=300;break;
                case 3:enteriseReqCount+=500;break;
                default:enteriseReqCount+=1000;break;
            }
        }
        data.put("EnterpriseReqCount",enteriseReqCount);
        //企业申请次数/总金额
        //企业申请成功次数
        //企业申请失败次数
        //企业申请正在处理中次数
        Map<String,Integer> finacialProductMap=new LinkedHashMap<>();
        for(FinancialProduct financialProduct:financialProductList){
            switch (financialProduct.getLoanLimit()){
                case 0:finacialProductMap.put(financialProduct.getProductName(),100);break;
                case 1:finacialProductMap.put(financialProduct.getProductName(),200);break;
                case 2:finacialProductMap.put(financialProduct.getProductName(),300);break;
                case 3:finacialProductMap.put(financialProduct.getProductName(),500);break;
                default:finacialProductMap.put(financialProduct.getProductName(),1000);break;
            }
        }
        QueryWrapper<ProductRequest> productRequestQueryWrapper=new QueryWrapper<>();
        List<ProductRequest> productRequestList=new ArrayList<>();
        productRequestList=productRequestMapper.selectList(productRequestQueryWrapper);
        data.put("ProductRequesNum",productRequestList.size());
        int productRequesCount=0;
        int productRequesSuccessNum=0,productRequesSuccessCount=0;
        int productRequesFailNum=0,productRequesFailCount=0;
        int productRequesProcessNum=0,productRequesProcessCount=0;
        for(ProductRequest productRequest:productRequestList){
            switch (productRequest.getStatus()){
                case 0:{
                    productRequesProcessNum+=1;
                    productRequesProcessCount+=finacialProductMap.get(productRequest.getFkProductName());
                    break;
                }
                case 1:{
                    productRequesSuccessNum+=1;
                    productRequesProcessCount+=finacialProductMap.get(productRequest.getFkProductName());
                    break;
                }
                case 2:{
                    productRequesFailNum+=1;
                    productRequesFailCount+=finacialProductMap.get(productRequest.getFkProductName());
                    break;
                }
                default:break;
            }
            productRequesCount+=finacialProductMap.get(productRequest.getFkProductName());
        }
        data.put("ProductRequesCount",productRequesCount);
        data.put("ProductRequesProcessNum",productRequesProcessNum);
        data.put("ProductRequesProcessCount",productRequesProcessCount);
        data.put("ProductRequesSuccessNum",productRequesSuccessNum);
        data.put("ProductRequesSuccessCount",productRequesSuccessCount);
        data.put("ProductRequesFailNum",productRequesFailNum);
        data.put("ProductRequesFailCount",productRequesFailCount);
        responseResult.setCode(GovernmentCode.CHECK_SUCCESS.getCode());
        responseResult.setMessage(GovernmentCode.CHECK_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setData(data);
        return responseResult;
    }

    @Override
    public ResponseResult getRequestInfo(int pageNum, int pageSize) throws Exception{
        ResponseResult responseResult=new ResponseResult();
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<ProductRequest> productRequestList=productRequestMapper.selectList(null);
            List<Map<String,Object>> list=new ArrayList<>();
            for(ProductRequest productRequest:productRequestList){
                Map<String,Object> object=new HashMap<>(13);
                object.put("SocialCode",productRequest.getFkSocialCode());
                QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper=new QueryWrapper<>();
                enterpriseInfoQueryWrapper.eq("social_code",productRequest.getFkSocialCode());
                EnterpriseInfo enterpriseInfo=enterpriseInfoMapper.selectOne(enterpriseInfoQueryWrapper);
                object.put("EnterpriseName",enterpriseInfo.getEnterpriseName());
                QueryWrapper<FinancialProduct> financialProductQueryWrapper=new QueryWrapper<>();
                financialProductQueryWrapper.eq("product_name",productRequest.getFkProductName());
                FinancialProduct financialProduct=financialProductMapper.selectOne(financialProductQueryWrapper);
                object.put("ProductName",financialProduct.getProductName());
                object.put("BankId",financialProduct.getFkBankId());
                object.put("BankName",financialProduct.getBankName());
                object.put("LoanLimit",financialProduct.getLoanLimit());
                object.put("RateUpperLimit",financialProduct.getRateUpperLimit());
                object.put("RateLowerLimit",financialProduct.getRateLowerLimit());
                object.put("LoanPeriod",financialProduct.getLoanPeriod());
                object.put("Comment",productRequest.getComment());
                object.put("FundUsage",productRequest.getFundUsage());
                object.put("Status",productRequest.getStatus());
                object.put("RequestTime",productRequest.getRequestTime());
                list.add(object);
            }
            Map<String,Object> data=new HashMap<>();
            PageInfo pageInfo=new PageInfo<> (list);
            data.put("EnterpriseRequestList",list);
            data.put("Total Page Num",pageInfo.getPages());
            data.put("Total",pageInfo.getTotal());
            responseResult.setCode(GovernmentCode.NAME_REQ_SUCCESS.getCode());
            responseResult.setMessage(GovernmentCode.NAME_REQ_SUCCESS.getMessage());
            responseResult.setData(data);
            responseResult.setSuccess(true);
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(GovernmentCode.NAME_REQ_FAIL.getCode());
            responseResult.setMessage(GovernmentCode.NAME_REQ_FAIL.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }
    }
}
